gen party2 = 0 if party== "BRICS"
replace party2 = 1 if party== "G7"
replace party2 = 2 if party== "Other"

egen centroid1 = mean(coord1d), by(party2)
egen centroid2 = mean(coord2d), by(party2)

gen edist= sqrt((( coord1d- centroid1)^2)+(( coord2d- centroid2)^2)) if coord1d != .

*centroid  coord1d coord2d, group(party2)

egen meanedist = mean(edist), by(party2)


***STOP HERE***

tab party, gen(group)

rename group1 brics
rename group2 g7

scatter coord2 coord1 if brics | g7, mlabel(ccode)

gen bricsplus = 0
replace bricsplus = 1 if ccode == 651 | ccode == 850 | ccode == 630 | ccode == 640 | ccode == 820 | ccode == 70 | ccode == 475 | ccode == 840 | ccode == 800

gen reggroup = 0
replace reggroup = 1 if ccode == 816 | ccode == 663 | ccode == 160 | ccode == 625 | ccode == 40 | ccode == 670 | ccode == 501 | ccode == 731 | ccode == 666
replace party2 = 3 if reggroup == 1 | bricsplus == 1

twoway (scatter coord2d coord1d if brics, msymbol(triangle) mlabel(stateabb)) /* 
*/(scatter coord2d coord1d if g7, mlabel(stateabb)) (scatter coord2d coord1d /*
*/if party2 == 2, mcolor(gray) msymbol(circle_hollow)) (scatter centroid2 /* 
*/centroid1 if party2 < 2, msymbol(lgx)) (scatter coord2d coord1d if /* 
*/bricsplus == 1, mcolor(green) msymbol(triangle_hollow) mlabel(stateabb) mlabcolor(green))/* 
*/(scatter coord2d coord1d if reggroup == 1, mcolor(gray)/* 
*/msymbol(circle_hollow) mlabel(stateabb) mlabsize(small) /*
*/ mlabangle(forty_five)), yscale(range(-1 1)) xscale(range(-1 1))

twoway (scatter coord2d coord1d if brics, msymbol(triangle) mlabel(stateabb) mlabsize(small)) /* 
*/(scatter coord2d coord1d if g7, mlabel(stateabb) mlabsize(small)) (scatter coord2d coord1d /*
*/if party2 == 2, mcolor(gray) msymbol(circle_hollow)) (scatter centroid2 /* 
*/centroid1 if party2 < 2, msymbol(lgx)) (scatter coord2d coord1d if /* 
*/bricsplus == 1, mcolor(green) msymbol(triangle_hollow) mlabel(stateabb) mlabcolor(green) mlabsize(small)),/* 
*/ yscale(range(-1 1)) xscale(range(-1 1))


**Create IBSA grouping**

gen party3 = 0 if party== "BRICS"
replace party3 = 1 if party== "G7"
replace party3 = 2 if party== "Other"

replace party3 = 2 if ccode == 710 | ccode == 365

gen IBSA_state = party
replace IBSA_state = "IBSA" if party3 == 0
replace IBSA_state = "Other" if party3 == 2

gen ibsa = 0  
replace ibsa = 1 if party3 == 0

egen centroid1_ibsa = mean(coord1d), by(ibsa)
egen centroid2_ibsa = mean(coord2d), by(ibsa)

gen edist_ibsa= sqrt((( coord1d- centroid1)^2)+(( coord2d- centroid2)^2)) if coord1d != .

*centroid  coord1d coord2d, group(ibsa)

egen meanedist_ibsa = mean(edist), by(ibsa)

*geographic groupings (not used)

gen islam = 1 if ccode >599 & ccode < 700
gen north = 1 if ccode == 2 | ccode == 20 | ccode == 305 | ccode == 325 | ccode == 338 | ccode == 350
replace north = 1 if ccode > 165 & ccode < 265
replace north = 1 if ccode >373 & ccode < 400

gen south = 1 if north != 1
replace south = 0 if south != 1
replace south = 0 if ccode == 265 | ccode == 290 | ccode == 310 | ccode ==315 | ccode==365 | ccode == 740 | ccode == 900 | ccode == 920

**STOP HERE**


*This part generates different BRICS and G7 groupings for robustness checks

gen party3 = 0 if party== "BRICS"
replace party3 = 1 if party== "G7"
replace party3 = 2 if party== "Other"
replace party3 = 2 if ccode== 710
replace party3 = 2 if ccode== 2

egen centroid1b = mean(coord1d), by(party3)
egen centroid2b = mean(coord2d), by(party3)

gen edistb= sqrt((( coord1d- centroid1b)^2)+(( coord2d- centroid2b)^2)) if coord1d != .

*centroid  coord1d coord2d, group(party3)

egen meanedistb = mean(edistb), by(party3)

gen party4 = 2 
replace party4 = 1 if ccode == 2 | ccode == 200 | ccode == 220
replace party4 = 0 if ccode == 365 | ccode == 750 | ccode == 710 


egen centroid1c = mean(coord1d), by(party4)
egen centroid2c = mean(coord2d), by(party4)

gen edistc= sqrt((( coord1d- centroid1c)^2)+(( coord2d- centroid2c)^2)) if coord1d != .

*centroid  coord1d coord2d, group(party3)

egen meanedistc = mean(edistc), by(party4)



gen party5 = 2 
replace party5 = 1 if party == "G7"
replace party5 = 0 if ccode == 140 | ccode == 750 | ccode == 710 


egen centroid1d = mean(coord1d), by(party5)
egen centroid2d = mean(coord2d), by(party5)

gen edistd= sqrt((( coord1d- centroid1d)^2)+(( coord2d- centroid2d)^2)) if coord1d != .

*centroid  coord1d coord2d, group(party3)

egen meanedistd = mean(edistd), by(party5)

gen bics = 2 
replace bics = 1 if party == "G7"
replace bics = 0 if ccode == 140 | ccode == 750 | ccode == 710 | ccode == 560 


egen centroid1e = mean(coord1d), by(bics)
egen centroid2e = mean(coord2d), by(bics)

gen ediste= sqrt((( coord1d- centroid1e)^2)+(( coord2d- centroid2e)^2)) if coord1d != .

*centroid  coord1d coord2d, group(party3)

egen meanediste = mean(ediste), by(bics)
